package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Admin;
import pojo.Classroom;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class AdminDaoImpl implements AdminDao{

    private Log log = LogFactory.getLog(getClass());
    //管理员信息
    @Override
    public ArrayList<Admin> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<Admin> list = new ArrayList<Admin>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM manager ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                Admin admin = new Admin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getBoolean(5));
                list.add(admin);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public Admin findByAdminAcount(Integer account) {
        log.info(SystemEnum.BEGIN);
        Admin admin = new Admin();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM manager WHERE account = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,account);
            rs = statement.executeQuery();
            while(rs.next()){
                admin.setAccount(rs.getInt(1));
                admin.setPassword(rs.getString(2));
                admin.setManaName(rs.getString(3));
                admin.setManaTel(rs.getString(4));
                admin.setSuper(rs.getBoolean(5));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return admin;
    }

    @Override
    public int insert(Admin admin) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "INSERT INTO manager VALUES (?,?,?,?,?) ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,admin.getAccount());
            statement.setString(2,admin.getPassword());
            statement.setString(3,admin.getManaName());
            statement.setString(4,admin.getManaTel());
            statement.setBoolean(5,admin.isSuper());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int update(Admin admin) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE manager SET password = ? , manaName = ?, manaTel = ? ,isSuper = ? WHERE account = ?";
            statement = con.prepareStatement(sql);
            statement.setString(1,admin.getPassword());
            statement.setString(2,admin.getManaName());
            statement.setString(3,admin.getManaTel());
            statement.setBoolean(4,admin.isSuper());
            statement.setInt(5,admin.getAccount());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int delete(Integer account) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "DELETE FROM manager  WHERE account = ?  ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,account);
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public boolean updatePermissions(Integer account) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE manager SET isSuper = !isSuper WHERE account = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,account);
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result!=0?true:false;
    }
}
